<?php
defined('IN_MAIN') or exit('Invalid Access!');

function get_member_matrix_leg($uid, $volume_type, $date_from, $date_to){
/*
This function will get total leg of input uid (user ID). The leg will be from the 1st level of user's matrix 
network, and the volume will include that 1st level matrix downline together with that 1st level's group volume.
Volume is based on the donwline's first sale only and subsequent upgrade of member rank.

Each volume is separated for its own total, and leg will be ordered from largest to smallest based on accumulated
volume for each leg.
*/

	global $vars;
	$db=$vars["db"];
	
	$calc_date['start'] = date("Y-m-d H:i:s", strtotime($date_from));
	$calc_date['end'] = date("Y-m-d H:i:s", strtotime($date_to));
	$one_day_before_date_from = date('Y-m-d', strtotime($date_from)-86400);
	
	//downlines = get all 1st level matrix downlines of uid, order by matrix position
	$t=@mysql_num_rows($r=mysql_query($s="select id, matrix_position from $db->users where matrix_upline='$uid' order by matrix_position"));
	//for each downlines
	$index=0;
	for($i=0;$i<$t;$i++){
		$member=mysql_fetch_assoc($r);
		//unset all_downlines
		unset($all_downlines);
		$all_downlines[$member["id"]]=$member;
		//uplines[] = this downline[id]
		$uplines[0]=$member["id"];
		//do
		do{
			//for each uplines
			$di=0;
			unset($downlinesk);
			for($j=0,$t2=count($uplines);$j<$t2;$j++){
				//upline_id = this upline[id]
				$upline_id=$uplines[$j];
				//downlines_id = get all 1st level matrix downline of upline_id
				$t3=@mysql_num_rows($r3=mysql_query($s="select id from $db->users where matrix_upline='$upline_id'"));
				//for each downlines_id
				for($k=0;$k<$t3;$k++){
					$downlinek=mysql_fetch_assoc($r3);
					$downlinesk[$di++]=$downlinek;
					//all_downlines[this downline[id]] = this downline
					$all_downlines[$downlinek["id"]]=$downlinek;
				}
			}
			//unset uplines
			unset($uplines);
			//for each downlines_id
			for($j=0,$t2=count($downlinesk);$j<$t2;$j++){
				//uplines[] = this downline[id]
				$uplines[$j] = $downlinesk[$j]["id"];
			}
		//end do while (uplines not empty)
		}while(count($uplines));
		
		$total_volume = 0;
		$total_volumea = 0;
		//for each all_downlines
		foreach($all_downlines as $did => $details){
			$total_unit = 0;
			$total_unita = 0;
			//foreach volume_type
			for($j=0,$t2=count($volume_type);$j<$t2;$j++){
				$carryforward = 0;
				//if all_downlines[this index] = this downline
				if($did == $member['id']){
					//carryforward = get any carry forward for this volume_type that is 1 day before the date_from (previous day)
					$carryforward = @mysql_result(mysql_query($s="select amount from $db->member_cf where uid='$did' and volume_type='$volume_type[$j]' and cdate='$one_day_before_date_from'"), 0) or 0;
					/*
						get the carry forward only for the 1st level downline of input uid since this 1st level downline carry forward already
						include for all the carryforward for all the downlines of this downline, during the pairing commission calculation.
						We don't need to get the carryforward for downlines of this downline since those downlines carry forward for their
						immediate matrix upline.
					*/
				}
				//sale_volume = get all sales volume that is of this volume_type and is confirmed and is the 1st sale for this downline, where sales date is between date_from and date_to
				$sale_volume = @mysql_result(mysql_query("select sum(total_bv) from $db->purchase where uid='$did' and first_sale='y' and total_bv='$volume_type[$j]' and status='confirmed' and cdate>='$calc_date[start]' and cdate<='$calc_date[end]'"), 0) or 0;
				$sale_unit = floor($sale_volume / $volume_type[$j]);
				$total_unit = $carryforward + $sale_unit; //include carry forward
				$total_unita = $sale_unit; //exclude carry forward
				$total_volume += $sale_volume + $carryforward * $volume_type[$j];
				$total_volumea += $sale_volume;
				$downlines_dd[$member['id']]['volume_type'][$volume_type[$j]]['unit'] += $total_unit;
				$downlines_dd[$member['id']]['volume_type'][$volume_type[$j]]['left_unit'] += $total_unit;
				$downlines_dd[$member['id']]['volume_type'][$volume_type[$j]]['aunit'] += $total_unita;
			}
		}
		$downlines_dd[$member['id']]['position'] = $member['matrix_position'];
		$downlines_dd[$member['id']]['total_volume'] = $total_volume;
		$downlines_dd[$member['id']]['total_avolume'] = $total_volumea;
		$downlines_id[$index]=$member["id"];
		$downlines_bv[$index]=$total_volume;
		$index++;
	}
	
	//sort downlines by ov, highest first to lowest
	if($downlines_id && $downlines_bv){
		array_multisort($downlines_bv, SORT_DESC, $downlines_id, SORT_ASC);
		for($i=0,$t=count($downlines_id);$i<$t;$i++){
			$downlines[$i]["id"]=$downlines_id[$i];
			$downlines[$i]['position'] = $downlines_dd[$downlines_id[$i]]['position'];
			$downlines[$i]["volume"]=$downlines_bv[$i];
			$downlines[$i]["avolume"]=$downlines_dd[$downlines_id[$i]]['total_avolume'];
			$downlines[$i]["volume_type"]=$downlines_dd[$downlines_id[$i]]['volume_type'];
		}
	}
	return $downlines;
}
?>